import "crvpuppet"

define at3_condor($webaddr="", $roleid="") {
  if $AutoMode==true and $CloudCRV==true {
    $condor_role_name=get_role_attr($webaddr,$roleid,"CondorRoleName")
    $condor_allow_negotiator_extra=get_role_attr($webaddr,$roleid,"CondorAllowNegotiatorExtra")
    $filesystemdomain=get_role_attr($webaddr,$roleid,"CondorFilesystemDomain")
    $CondorPassword=get_role_attr($webaddr,$roleid,"CondorPassword")
    
    if $condor_role_name == "head" {
    	$condorheadaddr=$ipaddress
    }
    else
    {
    	$condorheadaddr=get_provider_attr($webaddr,$roleid,"CondorHeadAddr")
    }
    
  }
  
  if ( $AutoMode == false ) or ( $AutoMode==true and $CloudCRV==true ) {
   notice("installing condor and augeas")
    package { ["augeas","condor"]:
      ensure => installed,
    }
  
    
    file { "/usr/share/augeas/lenses/condorconfig.aug":
      owner => "root",
      group => "root",
      content => template("at3_condor/condorconfig.aug"),
      mode => 755,
      require => Package["condor","augeas"],
    }
   
    
    augeas{"condor_config":
          require => [File["/usr/share/augeas/lenses/condorconfig.aug"]],
	  context => "/files",
	  changes => [
	    "set /files/etc/condor/condor_config/CONDOR_HOST $condorheadaddr",
	    "set /files/etc/condor/condor_config/IN_HIGHPORT 9999",
	    "set /files/etc/condor/condor_config/IN_LOWPORT 9000",
	    "set /files/etc/condor/condor_config/SEC_DAEMON_AUTHENTICATION required",
	    "set /files/etc/condor/condor_config/SEC_DAEMON_AUTHENTICATION_METHODS password",
	    "set /files/etc/condor/condor_config/SEC_CLIENT_AUTHENTICATION_METHODS password,fs,gsi,kerberos",
	    "set /files/etc/condor/condor_config/SEC_PASSWORD_FILE $(LOCAL_DIR)/condor_credential",
	    "set /files/etc/condor/condor_config/ALLOW_DAEMON condor_pool@*",
	    "set /files/etc/condor/condor_config/ALLOW_WRITE \$(CONDOR_HOST),*",
	    "set /files/etc/condor/condor_config/UID_DOMAIN \$(FULL_HOSTNAME)",
	    "set /files/etc/condor/condor_config/ALLOW_NEGOTIATOR \"$(CONDOR_HOST), $condor_allow_negotiator_extra\"",
	    "set /files/etc/condor/condor_config/ALLOW_NEGOTIATOR_SCHEDD \"$(CONDOR_HOST), $(FLOCK_NEGOTIATOR_HOSTS), $condor_allow_negotiator_extra\""
	  ],
	  load_path => "$/usr/share/augeas/lenses",
	  notify  => Service["condor"],  # this sets up the relationship
	}

    file { "/etc/condor/condor_config.local":
      owner => "root",
      group => "root",
      mode => 644,
      content => template("at3_condor/condor_config.local.${condor_role_name}.tpl"),
      notify  => Service["condor"],  # this sets up the relationship
    }

    exec { create_condorpassword:
      path => "/usr/bin:/usr/sbin/:/bin:/sbin:/usr/local/bin:/usr/local/sbin",
      command => "condor_store_cred -c add -p $CondorPassword",
      require => [Augeas["condor_config"],File["/etc/condor/condor_config.local"]],
      creates => "/var/condor_credentiald",
      notify  => Service["condor"],  # this sets up the relationship
    }
    
    if ( $condor_role_name == "worker"){
	    file { "/etc/claimed.sh":
	      owner => "root",
	      group => "root",
	      mode => 755,
	      content => template("at3_condor/claimed.sh.tpl"),
	    }
    }
    
    service { [ "condor"]:
      ensure => running,
      enable => true,
      hasstatus => true,
      hasrestart => true,
      require => [Exec["create_condorpassword"]],
    }
 }


  if $AutoMode==true and $CloudCRV==true {
    if $condor_role_name == "head" {
    	set_role_attr($webaddr,$roleid,"CondorHeadAddr","$ipaddress")    
    }
    #set_role_attr($webaddr,$roleid,"ProfileApplied","True")
  }
}


define at3_head_reverse($webaddr, $roleid) {
  #set_role_attr($webaddr,$roleid,"ReverseProfileApplied","True")
}

